﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ECCSystem
{
    public partial class frmUserMaintenance : Form
    {
        DataSet objDS = new DataSet();
        private BizRule.ClsUserMaintenance objBRUM;
        private BizRule.ClsSystemLog objSystemLog;
        public frmUserMaintenance()
        {
            InitializeComponent();
        }

        private void frmUserMaintenance_Load(object sender, EventArgs e)
        {
            try
            {
                //set default user role 
                cboRole.SelectedIndex = 0;
                //initialize controls
                btnEdit.Enabled = false;
                //Bind user list
                this.BindDataGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }
        }

        //Reset Conrols
        private void ResetControls()
        {
            txtLoginID.Text = string.Empty;
            txtLogonName.Enabled = true;
            txtLogonName.Text = string.Empty;
            cboRole.SelectedIndex = 0;
            txtUserName.Text = string.Empty;
            btnSave.Enabled = true;
            btnEdit.Enabled = false;
            btnDelete.Enabled = true;
            //reset this hided control,after finshing to edit the user
            hidID.Text = "";
            hidUserRole.Text = "";
        }

        private void frmUserMaintenance_FormClosing(object sender, FormClosingEventArgs e)
        {
            Form frmMain = new frmMain();
            frmMain.Show();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.CheckInputItem() == true)
                {
                    //If Login ID is existence or not
                    long lngID = 0;//show it is add_click
                    objBRUM = new ECCSystem.BizRule.ClsUserMaintenance();
                    if (objBRUM.CheckUserLoginID(lngID, txtLoginID.Text.ToString().Trim()) == true)
                    {
                        MessageBox.Show("このLogin ID既に存在しています！", "ECC Information");
                        return;
                    }
                    //If Logon Name is existence or not
                    if (objBRUM.CheckUserLogonName(txtLogonName.Text.ToString().Trim()) == true)
                    {
                        MessageBox.Show("この登録名既に存在しています！", "ECC Information");
                        return;
                    }
                    //while userRole is "Admin",if have existed 2 admin user or not
                    if (cboRole.Text.ToString() == "Admin")
                    {
                        if (objBRUM.CheckAdminUser() == true)
                        {
                            MessageBox.Show("Adminユーザー二個以上にならないでください！", "ECC Information");
                            return;
                        }
                    }
                    objBRUM.AddUser(txtLoginID.Text.ToString().Trim(), txtLogonName.Text.ToString().Trim(), cboRole.Text, txtUserName.Text.ToString().Trim());
                    objSystemLog = new ECCSystem.BizRule.ClsSystemLog();
                    objSystemLog.AddSystemLog("Add a new user-" + txtLogonName.Text.ToString());
                    this.BindDataGrid();
                    this.ResetControls();

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.CheckInputItem() == true)
                {
                    objBRUM = new ECCSystem.BizRule.ClsUserMaintenance();
                    //If Login ID is existence or not
                    if (objBRUM.CheckUserLoginID(Convert.ToInt64(hidID.Text.ToString()), txtLoginID.Text.ToString().Trim()) == true)
                    {
                        MessageBox.Show("このLogin ID既に存在しています！", "ECC Information");
                        return;
                    }
                    //while userRole is "Admin",if have existed 2 admin user or not
                    if (hidUserRole.Text.ToString() != "Admin" && cboRole.Text.ToString() == "Admin")
                    {
                        if (objBRUM.CheckAdminUser() == true)
                        {

                            MessageBox.Show("Adminユーザー二個以上にならないでください！", "ECC Information");
                            return;
                        }
                    }
                    objBRUM.EditUser(Convert.ToInt64(hidID.Text.ToString()), txtLoginID.Text.ToString().Trim(), txtLogonName.Text.ToString().Trim(), cboRole.Text, txtUserName.Text.ToString().Trim());
                    objSystemLog = new ECCSystem.BizRule.ClsSystemLog();
                    objSystemLog.AddSystemLog("Edit the user-" + hidID.Text.ToString());
                    this.BindDataGrid();
                    this.ResetControls();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgList.CurrentRow == null)
                {
                    MessageBox.Show("データを選んでください！", "ECC Information");
                    return;
                }
                if (Convert.ToInt32(dgList[0, dgList.CurrentRow.Index].Value.ToString()) == BizRule.ClsCommon.gIntOperatorID)
                {
                    MessageBox.Show("自身を削除しないでください！", "ECC Information");
                    return;
                }
                if (MessageBox.Show("削除します、よろしいですか？", "ECC Information", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    objBRUM = new ECCSystem.BizRule.ClsUserMaintenance();
                    objBRUM.DeleteUser(Convert.ToInt64(dgList[0, dgList.CurrentRow.Index].Value.ToString()));
                    objSystemLog = new ECCSystem.BizRule.ClsSystemLog();
                    objSystemLog.AddSystemLog("Delete the user-" + dgList[0, dgList.CurrentRow.Index].Value.ToString());
                    this.BindDataGrid();
                    this.ResetControls();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }
        }
        //Bind data
        private void BindDataGrid()
        {
            objBRUM = new ECCSystem.BizRule.ClsUserMaintenance();
            objDS = (DataSet)objBRUM.GetList();
            dgList.DataSource = objDS.Tables[0];
        }

        //Validate Data 
        private bool CheckInputItem()
        {
            BizRule.ClsCommon objCommon = new ECCSystem.BizRule.ClsCommon();
            if (txtLoginID.Text.ToString().Trim() == "")
            {
                MessageBox.Show("Login IDを入力してください！", "ECC Information");
                txtLoginID.Focus();
                return false;
            }
            if (txtLogonName.Text.ToString().Trim() == "")
            {
                MessageBox.Show("登録名を入力してください！", "ECC Information");
                txtLogonName.Focus();
                return false;
            }
            if (objCommon.CheckHalfSpaceStr(txtLogonName.Text.ToString().Trim()) == false)
            {
                MessageBox.Show("登録名は半角英数で入力してください！", "ECC Information");
                txtLogonName.Focus();
                return false;
            }
            if (txtUserName.Text.ToString().Trim() == "")
            {
                MessageBox.Show("名前を入力してください！", "ECC Information");
                txtUserName.Focus();
                return false;
            }
            return true;
        }

        //only can input number by keyboard
        private void txtLoginID_KeyPress(object sender, KeyPressEventArgs e)
        {
            BizRule.ClsCommon objCommon = new ECCSystem.BizRule.ClsCommon();
            objCommon.TextKeyPress(e);
        }

        //
        private void dgList_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                long lngID;
                //get this record's auto id
                lngID = Convert.ToInt64(dgList[0, dgList.CurrentRow.Index].Value.ToString());
                this.FillDataControls(lngID);
                //set controls status
                txtLogonName.Enabled = false;
                btnSave.Enabled = false;
                btnDelete.Enabled = false;
                btnEdit.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }
        }

        //fill selected user info into controls
        private void FillDataControls(long lngID)
        {

            for (int i = 0; i < objDS.Tables[0].Rows.Count; i++)
            {
                if (objDS.Tables[0].Rows[i][0].ToString() == lngID.ToString())
                {
                    txtLoginID.Text = objDS.Tables[0].Rows[i][1].ToString();
                    txtLogonName.Text = objDS.Tables[0].Rows[i][2].ToString();
                    cboRole.Text = objDS.Tables[0].Rows[i][3].ToString();
                    txtUserName.Text = objDS.Tables[0].Rows[i][4].ToString();
                    //for editing this record,hided controls
                    hidID.Text = objDS.Tables[0].Rows[i][0].ToString();
                    hidUserRole.Text = objDS.Tables[0].Rows[i][3].ToString();
                }
            }
        }
    }
}